package com.weixing.mall.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.provider.mapper.AgentRuleMapper;
import com.weixing.mall.provider.model.domain.AgentRule;
import com.weixing.mall.provider.model.dto.AgentRuleDto;
import com.weixing.mall.provider.model.enums.AgentRuleStatusEnum;
import com.weixing.mall.provider.model.query.AgentRuleQuery;
import com.weixing.mall.provider.model.vo.AgentRuleVo;
import com.weixing.mall.provider.service.IAgentRuleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @ClassName AgentRuleServiceImpl
 * @Description TODO <br/>
 * @Author gjq
 * @Modified By:
 * @Date 2019/9/23
 * @Version 1.0
 **/

/**
 * <p>
 * 代理商模块-代理商晋升规则表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-18
 */
@Service
public class AgentRuleServiceImpl extends ServiceImpl<AgentRuleMapper, AgentRule> implements IAgentRuleService {

    @Override
    public IPage<AgentRuleVo> selectOfPage(Page page, AgentRuleQuery agentRuleQuery) {
        return baseMapper.selectListOfPage(page, agentRuleQuery);
    }


    @Override
    public void save(AgentRuleDto agentRuleDto) {

        AgentRule agentRule = new AgentRule();
        BeanUtils.copyProperties(agentRuleDto, agentRule);
        agentRule.setAgentType(agentRuleDto.getAgentType().getValue());
        agentRule.setAgentLevel(agentRuleDto.getAgentLevel().getValue());
        agentRule.setRuleStatus(agentRuleDto.getRuleStatus().getValue());
        if (null == agentRuleDto.getId()) {
            agentRule.setCreatedTime(new Date());
            save(agentRule);
        } else {
            updateById(agentRule);
        }
    }


    @Override
    public AgentRule selectByRuleName(String ruleName) {
        return getOne(new QueryWrapper<AgentRule>().eq("rule_name", ruleName));
    }

    @Override
    public AgentRule selectByLevelAndType(int agentLevel, int agentType) {
        return getOne(new QueryWrapper<AgentRule>().eq("agent_level", agentLevel).eq("agent_type", agentType));
    }
}
